//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    /// <summary>
    /// Interaction logic for RegistryManagerLeftPaneView.xaml
    /// </summary>
    public sealed partial class RegistryManagerLeftPaneView : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public RegistryManagerLeftPaneView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemContainer_Expanded(object sender, RoutedEventArgs e)
        {
            Expander selectedExpander = sender as Expander;
            (this.DataContext as RegistryManagerLeftPanePresenter).ItemExpanded.Execute(selectedExpander);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemContainer_Loaded(object sender, RoutedEventArgs e)
        {
            Expander selectedExpander = sender as Expander;
            RegistryManagerLeftPaneItemModel model = selectedExpander.DataContext as RegistryManagerLeftPaneItemModel;
            if (model.IsExpanded)
                selectedExpander.IsExpanded = true;
        }

        /// <summary>
        /// Add the content to the search pane.
        /// </summary>
        /// <param name="searchControl">The search pane.</param>
        public void AddToSearchPane(UIElement searchControl)
        {
            this.SearchControlHolder.Children.Clear();
            this.SearchControlHolder.Children.Add(searchControl);
        }

        private void OnRefreshClick(object sender, RoutedEventArgs e)
        {
            try
            {
                Button refreshButton = sender as Button;
                if (refreshButton != null && refreshButton.Parent != null)
                {
                    Grid grid = refreshButton.Parent as Grid;
                    Expander expander = grid.Children[0] as Expander;
                    if (expander != null)
                    {
                        (this.DataContext as RegistryManagerLeftPanePresenter).ItemRefreshed.Execute(expander);
                    }
                }
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleUIException(ex);
                TridentMessageBox.ShowTridentErrorMessageBox(ManagementStudioResourceManager.GetString("WorkflowRefreshException"));
            }
        }
    }
}
